/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.costs.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.thinkgem.jeesite.common.beanvalidator.BeanValidators;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.utils.excel.ImportExcel;
import com.thinkgem.jeesite.modules.costs.entity.WarehouseLaborCosts;
import com.thinkgem.jeesite.modules.costs.service.WarehouseLaborCostsService;

/**
 * 仓库人工成本Controller
 * 
 * @author hui.li
 * @version 2017-03-13
 */
@Controller
@RequestMapping(value = "${adminPath}/costs/warehouseLaborCosts")
public class WarehouseLaborCostsController extends BaseController {

	@Autowired
	private WarehouseLaborCostsService warehouseLaborCostsService;

	@ModelAttribute
	public WarehouseLaborCosts get(@RequestParam(required = false) String id) {
		WarehouseLaborCosts entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = warehouseLaborCostsService.get(id);
		}
		if (entity == null) {
			entity = new WarehouseLaborCosts();
		}
		return entity;
	}

	@RequiresPermissions("costs:warehouseLaborCosts:view")
	@RequestMapping(value = { "list", "" })
	public String list(WarehouseLaborCosts warehouseLaborCosts,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Page<WarehouseLaborCosts> page = warehouseLaborCostsService.findPage(
				new Page<WarehouseLaborCosts>(request, response),
				warehouseLaborCosts);
		model.addAttribute("page", page);
		return "modules/costs/warehouseLaborCostsList";
	}

	@RequiresPermissions("costs:warehouseLaborCosts:view")
	@RequestMapping(value = "form")
	public String form(WarehouseLaborCosts warehouseLaborCosts, Model model) {
		model.addAttribute("warehouseLaborCosts", warehouseLaborCosts);
		return "modules/costs/warehouseLaborCostsForm";
	}

	@RequiresPermissions("costs:warehouseLaborCosts:edit")
	@RequestMapping(value = "save")
	public String save(WarehouseLaborCosts warehouseLaborCosts, Model model,
			RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, warehouseLaborCosts)) {
			return form(warehouseLaborCosts, model);
		}
		warehouseLaborCostsService.save(warehouseLaborCosts);
		addMessage(redirectAttributes, "保存仓库人工成本成功");
		return "redirect:" + Global.getAdminPath()
				+ "/costs/warehouseLaborCosts/?repage";
	}

	@RequiresPermissions("costs:warehouseLaborCosts:edit")
	@RequestMapping(value = "delete")
	public String delete(WarehouseLaborCosts warehouseLaborCosts,
			RedirectAttributes redirectAttributes) {
		warehouseLaborCostsService.delete(warehouseLaborCosts);
		addMessage(redirectAttributes, "删除仓库人工成本成功");
		return "redirect:" + Global.getAdminPath()
				+ "/costs/warehouseLaborCosts/?repage";
	}

	/**
	 * 导入仓库人工成本
	 * 
	 * @param file
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("costs:warehouseLaborCosts:edit")
	@RequestMapping(value = "import", method = RequestMethod.POST)
	public String importFile(MultipartFile file,
			RedirectAttributes redirectAttributes) {
		if (Global.isDemoMode()) {
			addMessage(redirectAttributes, "演示模式，不允许操作！");
			return "redirect:" + Global.getAdminPath()
					+ "/costs/warehouseLaborCosts/list?repage";
		}
		try {
			StringBuilder failureMsg = new StringBuilder();
			ImportExcel ei = new ImportExcel(file, 0, 0);
			List<WarehouseLaborCosts> list = ei
					.getDataList(WarehouseLaborCosts.class);
			for (WarehouseLaborCosts cost : list) {
				try {
					BeanValidators.validateWithException(validator, cost);
					warehouseLaborCostsService.save(cost);
				} catch (ConstraintViolationException ex) {
					failureMsg.append(" 导入失败：");
					continue;
				} catch (Exception ex) {
					failureMsg.append("导入失败：" + ex.getMessage());
					continue;
				}
			}
			addMessage(redirectAttributes, "已成功导入 ");
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入仓库人工成本失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath()
				+ "/costs/warehouseLaborCosts/list?repage";
	}

	/**
	 * 下载导入 仓库人工成本模板
	 * 
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("costs:warehouseLaborCosts:view")
	@RequestMapping(value = "import/template")
	public String importFileTemplate(HttpServletResponse response,
			RedirectAttributes redirectAttributes) {
		try {
			String fileName = "仓库人工成本.xlsx";
			List<WarehouseLaborCosts> list = Lists.newArrayList();
			list.add(warehouseLaborCostsService.get("22"));
			new ExportExcel("仓库人工成本", WarehouseLaborCosts.class, 2).setDataList(list)
					.write(response, fileName).dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath()
				+ "/costs/warehouseLaborCosts/list?repage";
	}

	/**
	 * 导出仓库人工成本数据
	 * 
	 * @param user
	 * @param request
	 * @param response
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("costs:warehouseLaborCosts:view")
	@RequestMapping(value = "export", method = RequestMethod.POST)
	public String exportFile(
			HttpServletRequest request, HttpServletResponse response,
			RedirectAttributes redirectAttributes,WarehouseLaborCosts warehouseLaborCosts) {
		try {
			String fileName = "国内仓库仓库人工成本数据" + DateUtils.getDate("yyyyMMddHHmmss")
					+ ".xlsx";
			Page<WarehouseLaborCosts> page = warehouseLaborCostsService
					.findPage(new Page<WarehouseLaborCosts>(request, response, -1),
							warehouseLaborCosts);
			new ExportExcel("国内仓库仓库人工成本数据", WarehouseLaborCosts.class)
					.setDataList(page.getList()).write(response, fileName)
					.dispose();
			return null;
		} catch (Exception e) {
			addMessage(redirectAttributes, "导出国内仓库仓库人工成本失败！失败信息：" + e.getMessage());
		}
		return "redirect:" + Global.getAdminPath()
				+ "/costs/warehouseLaborCosts/?repage";
	}

}